package frmsystem.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.xml.ws.Action;

import frmsystem.Domain;
import frmsystem.IDomain;

public class FrameworkController {
	
	protected MainUI mainUI;
	protected OperationPanel operationPanel;
	protected TransactionButtonPanel transactionButtonPanel;
	protected JTable table;
	protected int noOfEmp;
	protected int accountType;
	protected PersonalRegistrationGUI personalGui;
	protected CompanyRegistrationGUI companyGui;
	protected TransactionUI transUI;
	protected IDomain domain;
	protected ReportUI report;
	
	public FrameworkController() {
	}

	public FrameworkController(int width, int height){
		
		domain = Domain.getInstance();
		
		operationPanel = new OperationPanel(580, 50);
		operationPanel.setAddCompanyAccountListener(new CompanyAccountListener());
		operationPanel.setAddPersonalAccountListener(new PersonalAccountListener());
		operationPanel.setAddInterestListener(new InterestListener());
		
		transactionButtonPanel = new TransactionButtonPanel(100, 380);
		transactionButtonPanel.addTransactionListener(new TransactionListener());
		transactionButtonPanel.addExitListener(new ExitListener());
		
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		mainUI = new MainUI(operationPanel, transactionButtonPanel, table, width, height);
		
		mainUI.setVisible(true);
	}

	protected class PersonalAccountListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			personalGui = new PersonalRegistrationGUI();
			personalGui.addOkActionListener(new PersonalOkListener());
			personalGui.addCancelActionListener(new PersonalCancelListener());
			personalGui.setVisible(true);
			
			//TODO remove this code later
			DefaultTableModel mod = new DefaultTableModel(domain.getAccountData(), domain.getTitles());
			table.setModel(mod);
			table.repaint();
		}
		
	}
	
	protected class CompanyAccountListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			personalGui = new PersonalRegistrationGUI();
			personalGui.addOkActionListener(new PersonalOkListener());
			personalGui.addCancelActionListener(new PersonalCancelListener());
			personalGui.setVisible(true);
		}
		
	}
	
	protected class InterestListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO add interest to the classes
			System.out.println("add interest");
		}
		
	}
	
	protected class TransactionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			int row = table.getSelectedRow();
			int col = table.getSelectedColumn();
			if(table.isCellSelected(row, col)){
				String val = (String)table.getValueAt(row, 0);
				String party = (String)table.getValueAt(row, 1);
				transUI = new TransactionUI(val, party);
				transUI.addOkActionListener(new TransactionOkListener());
				transUI.addCancelActionListener(new TransactionCancelListener());
				transUI.setVisible(true);
			}
			
		}
		
	}
	
	protected class ExitListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			System.exit(0);			
		}
		
	}
	
	protected class PersonalOkListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			//TODO add personal account
			System.out.println("add personal account");
			personalGui.dispose();
		}
		
	}
	
	protected class PersonalCancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			personalGui.dispose();			
		}
		
	}
	
	protected class TransactionOkListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			//TODO create transaction
			System.out.println("Transaction created");
		}
		
	}
	
	protected class TransactionCancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			transUI.dispose();
			
		}
		
	}

}
